package com.example.fw;

import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;


public class GroupHelper extends HelperWithWebDriverBase {
	
	private Groups groupModel;

	public GroupHelper(ApplicationManager manager) {
		super(manager);
	}
	
	/**
	 * Gets groups from cash or from database.
	 * @return
	 */
	public Groups getGroups() {
		if (groupModel == null) {
			groupModel = manager.getHibernateHelper().getGroups();
		}
		return groupModel;
	}
	
	public Groups getUiGroups() {
		HashSet<GroupObject> set = new HashSet<GroupObject>();
		manager.getNavigationHelper().goToGroupListPage();
		WebElement form = findElements(By.tagName("form")).get(1);
		List<WebElement> checkboxes = form.findElements(By.name("selected[]"));
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute("title");
			Pattern p = Pattern.compile(".*\\((.*)\\)");
			Matcher m = p.matcher(title);
			if (m.matches()) {
				title = m.group(1);			
			}
			GroupObject group = new GroupObject()
				.setName(title)
				.setId(checkbox.getAttribute("value"));
			set.add(group);
		}
		return new Groups(set);
	}	
	
	/**
	 * Creates group and add to cash(groupModel).
	 * @param validGroup
	 */
	public void createGroup(GroupObject validGroup) {
		manager.getNavigationHelper().goToGroupListPage();
		initGroupCreation();
		filGroupForm(validGroup);
		submitCreation();
		returnToGroupListPage();
		groupModel = groupModel.withAdded(validGroup);
	}
	
	//delete group by Id.
	public void deleteGroup(String idGroup) {
		manager.getNavigationHelper().goToGroupListPage();
		checkGroup(idGroup);
		click(By.name("delete"));
		returnToGroupListPage();
		groupModel = groupModel.without(idGroup);
	}	
	
	public void modifyGroup(String idGroup, GroupObject validGroup) {
		manager.getNavigationHelper().goToGroupListPage();
		checkGroup(idGroup);
    	initGroupModification();
		filGroupForm(validGroup);
		submitModification();
		returnToGroupListPage();
		groupModel = groupModel.without(idGroup);
	}	

	/**
	 * Gets group name.
	 * @param id
	 * @return group name
	 */
	public String getNameGroupById(String id){
		WebElement row  = findElement(By.xpath("//input[@value="+id+"]"));
		String nameGroup = row.getAttribute("title");
		return nameGroup;
	}
	
	private void initGroupCreation() {
		click(By.name("new"));
	}

	private void initGroupModification() {
		click(By.name("edit"));
	}
	
	/**
	 * Checks element by id.
	 * @param idGroup
	 */
	private void checkGroup(String idGroup){
		WebElement checkbox =  findElement(By.xpath("//input[@value="+idGroup+"]"));
		checkbox.click();	
	}

	private void filGroupForm(GroupObject groupObject) {
		type(By.name("group_name"), groupObject.name);
		type(By.name("group_header"), groupObject.header);
		type(By.name("group_footer"), groupObject.footer);
	}
	
	private void returnToGroupListPage() {
		click(By.linkText("group page"));
	}
}